home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 576 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.0 KB

  1. From: jones@cais.cais.com (Ben Jones)
  2. Message-ID: <4h1pb6$n19@news2.cais.com>
  3. X-Original-Date: 28 Feb 1996 14:37:26 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 28 Feb 96 14:57:55 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: Generic Object Callbacks
  9. Organization: Capital Area Internet Service info@cais.com 703-448-4470
  10. References: <pgpmoose.199602221531.14635@isolde.mti.sgi.com> <4gsi2p$905@bcarh8ab.bnr.ca> <4guh9e$jt6@sdaw04.seinf.abb.se>
  11. X-Newsreader: TIN [version 1.2 PL2]
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMTRtfeEDnX0m9pzZAQFXcwF9EZmIpTCvdAugsxZYh5H3iOB0Gs/FZ+uK
  14.     dq+++QSKBFuVSMRotqEcOBnSvXrDyVaB
  15.     =2xdO
  16.  
  17. Anders Lindback (alindbac@sw.seisy.abb.se) wrote:
  18. : In article <4gsi2p$905@bcarh8ab.bnr.ca>,
  19. : brian (b.c.) white <bcwhite@bnr.ca> wrote:
  20. : >
  21. : >>What is needed is the ability to use member functions as callbacks
  22. : >>without any constraint on the type of the object they are invoked on.
  23. : >>This is possible in C++ only by using various implementation-dependent
  24. : >>hacks to escape the type system, because the language does not provide
  25. : >>any way to express such a construct.
  26. : >
  27. :
  28. : There exist a rather simple solution using a static member function. 
  29. : Instead of having the callback call a normal member function it can 
  30. : call a static callback member function with the object as a 
  31. : argument. Then this callback function can call the real callback function.
  32. : This will involve an extra callback function for each callback one has,
  33. : but on the other hand it involves no changes to the base class if
  34. : one want to override the real callback function in a derived class.
  35.  
  36. : class X
  37. : {
  38. : public:
  39. :   static void callback(void* to)
  40. :   virtual void real_callback();
  41. : };
  42.  
  43. : void X::callback(void* to)
  44. : { 
  45. :   X* me = (X*)to;
  46. :   me ->real_callback()
  47. : }
  48.  
  49. : Motif widget callbacks and event handlers are examples where this pattern
  50. : works without problems. 
  51.  
  52. : If the desired class Y can not be modified it is possible that X is a helper
  53. : class that knows which member function on the real class Y it shall call.
  54. : I.e. this solution is portable and can be used for all possible classes.
  55.  
  56. Granted, this works but it is still not typesafe since you are casting
  57. the object pointer to a void.  It also forces you to clutter your class
  58. definitions with extra functions in order to allow for the possibility
  59. of callbacks (you can also define global functions which do the same
  60. thing but then you clutter the global namespace).
  61.  
  62. The point we have been making is that there is a trivial way to extend
  63. the C++ language to provide for completely typesafe callbacks which
  64. are simple to use and which will dramatically improve the clarity
  65. of programs.
  66.  
  67. Ben Jones
  68. Hughes Information Technology
  69. bjones@eos.hitc.com
  70. ---
  71. [ To submit articles: try just posting with your news-reader.
  72.                       If that fails, use mailto:std-c++@ncar.ucar.edu
  73.   FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  74.   Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  75.   Comments? mailto:std-c++-request@ncar.ucar.edu.
  76. ]
  77.